﻿Imports System.Data
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Partial Class Empresas_Empresas
    Inherits PaginaComun

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim obj As New Empresas
        obj.ID = TxtId.Value
        obj.TIP_DOC = CmbTipId.SelectedValue
        obj.NOMBRE = TxtNom.Text
        obj.DIRECCION = TxtDir.Text
        obj.TELEFONO = TxtTel.Text
        obj.CELULAR = TxtCel.Text
        obj.FAX = TxtFax.Text
        obj.EMAIL = TxtEmail.Text
        obj.DPTO = CmbDptoUbi.SelectedValue
        obj.MUNiCIPIO = CmbMunUbi.SelectedValue
        obj.REP_LEGAL = TxtRepLeg.Text
        obj.CONTACTO = TxtContac.Text
        obj.CED_REP_LEGAL = TxtIdRep.Value
        obj.IDPK = Me.Pk1
        Select Case Me.Oper
            Case "Nuevo"
                MsgModalPanel.Text = obj.Insert
                If obj.lErrorG = False Then
                    ImgRst.ImageUrl = "~/imagenes/good.png"
                    Membership.CreateUser(Me.TxtId.Text, Me.TxtId.Text & ".")
                    Dim objPer As New DBMenu
                    Me.MsgModalPanel.Text += "<BR/>" + objPer.AsigPerfilUser("Empresas", Me.TxtId.Text)
                Else
                    ImgRst.ImageUrl = "~/imagenes/error.png"
                End If
                ModalPopup.Show()
            Case "Editar"
                MsgModalPanel.Text = obj.Update
                If obj.lErrorG = False Then
                    ImgRst.ImageUrl = "~/imagenes/good.png"
                Else
                    ImgRst.ImageUrl = "~/imagenes/error.png"
                End If
                ModalPopup.Show()
        End Select
        Me.GridView1.DataBind()
        Me.MultiView1.ActiveViewIndex = 1
    End Sub

    Sub limpiar()
        Me.TxtId.Text = ""
        Me.CmbTipId.Text = ""
        Me.TxtNom.Text = ""
        Me.TxtRepLeg.Text = ""
        Me.TxtDir.Text = ""
        Me.TxtTel.Text = ""
        Me.TxtCel.Text = ""
        Me.TxtFax.Text = ""
        Me.TxtEmail.Text = ""
        Me.CmbDptoUbi.Text = ""
        Me.CmbMunUbi.Text = ""
        Me.TxtContac.Text = ""
    End Sub

    Protected Sub ImageButton2_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) 'Handles ImageButton2.Click
        Me.MultiView1.ActiveViewIndex = 1
    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        Dim obj As New Empresas
        Select Case e.CommandName
            Case "Editar"
                Me.Oper = "Editar"
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                GridView1.SelectedIndex = index
                Dim dt As DataTable = obj.GetbyPK(GridView1.DataKeys(index).Values(0).ToString())
                If dt.Rows.Count > 0 Then
                    Me.TxtId.Text = dt.Rows(0)("id").ToString
                    Me.CmbTipId.SelectedValue = dt.Rows(0)("Tipo_Id").ToString
                    Me.TxtNom.Text = dt.Rows(0)("Nombre").ToString
                    Me.TxtRepLeg.Text = dt.Rows(0)("Rep_Legal").ToString
                    TxtIdRep.Text = dt.Rows(0)("Ced_Rep_Legal").ToString
                    Me.TxtDir.Text = dt.Rows(0)("Direccion").ToString
                    Me.TxtTel.Text = dt.Rows(0)("Telefono").ToString
                    Me.TxtCel.Text = dt.Rows(0)("Celular").ToString
                    Me.TxtFax.Text = dt.Rows(0)("Fax").ToString
                    Me.TxtEmail.Text = dt.Rows(0)("Email").ToString
                    Me.TxtContac.Text = dt.Rows(0)("Contacto").ToString
                    Me.CmbDptoUbi.SelectedValue = dt.Rows(0)("Dpto").ToString
                    Me.CmbMunUbi.SelectedValue = dt.Rows(0)("Municipio").ToString
                    Me.Pk1 = dt.Rows(0)("Id").ToString
                    Me.MultiView1.ActiveViewIndex = 0
                End If
            Case "Eliminar"
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                GridView1.SelectedIndex = index
                Me.Pk1 = GridView1.DataKeys(index).Values(0).ToString()
                Me.ModalPopupExtender1.Show()
        End Select
    End Sub

    Protected Sub ImageButton3_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton3.Click
        Me.Oper = "Nuevo"
        habilitar(True)
        limpiar()
        Me.MultiView1.ActiveViewIndex = 0
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim obj As New Personas
        MsgModalPanel.Text = obj.Delete(Me.Pk1)
        If obj.lErrorG = False Then
            ImgRst.ImageUrl = "~/imagenes/good.png"
        Else
            ImgRst.ImageUrl = "~/imagenes/error.png"
        End If
        ModalPopup.Show()
        GridView1.DataBind()
    End Sub

    Protected Sub BtnVolver_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BtnVolver.Click
        Me.MultiView1.ActiveViewIndex = 1
    End Sub

    Protected Sub TxtId_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtId.TextChanged
        If Me.Oper = "Nuevo" Then
            Dim obj As New Empresas
            Dim dt As Data.DataTable = obj.GetbyPK(TxtId.Text)
            If dt.Rows.Count > 0 Then
                MsgModalPanel.Text = "El sistema ha encontrado una Empresa registrada con el documento digitado"
                ImgRst.ImageUrl = "~/imagenes/dialog_warning.png"
                ModalPopup.Show()
                Me.TxtId.Text = dt.Rows(0)("id").ToString
                Me.CmbTipId.SelectedValue = dt.Rows(0)("Tipo_Id").ToString
                Me.TxtNom.Text = dt.Rows(0)("Nombre").ToString
                Me.TxtRepLeg.Text = dt.Rows(0)("Rep_Legal").ToString
                Me.TxtDir.Text = dt.Rows(0)("Direccion").ToString
                Me.TxtTel.Text = dt.Rows(0)("Telefono").ToString
                Me.TxtCel.Text = dt.Rows(0)("Celular").ToString
                Me.TxtFax.Text = dt.Rows(0)("Fax").ToString
                Me.TxtEmail.Text = dt.Rows(0)("Email").ToString
                Me.TxtContac.Text = dt.Rows(0)("Contacto").ToString
                Me.CmbDptoUbi.SelectedValue = dt.Rows(0)("Dpto").ToString
                Me.CmbMunUbi.SelectedValue = dt.Rows(0)("Municipio").ToString
                Me.Pk1 = dt.Rows(0)("Id").ToString
                Me.MultiView1.ActiveViewIndex = 0
                habilitar(False)
                Me.ImageButton1.Enabled = False
            End If
        End If
    End Sub
    Sub habilitar(ByVal v As Boolean)
        Me.TxtId.Enabled = v
        Me.CmbTipId.Enabled = v
        Me.TxtNom.Enabled = v
        Me.TxtRepLeg.Enabled = v
        Me.TxtDir.Enabled = v
        Me.TxtTel.Enabled = v
        Me.TxtCel.Enabled = v
        Me.TxtFax.Enabled = v
        Me.TxtEmail.Enabled = v
        Me.CmbDptoUbi.Enabled = v
        Me.CmbMunUbi.Enabled = v
        Me.TxtContac.Enabled = v
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            
        End If
    End Sub

    Protected Sub Up_Ctrl_Personas1_SelClicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles Up_Ctrl_Personas1.SelClicked
        Me.Up_Ctrl_Personas1.Ocultar()
        Me.HfPK.Value = Me.Up_Ctrl_Personas1.Identificacion
        Me.TxtRepLeg.Text = Me.Up_Ctrl_Personas1.Nombre
    End Sub

    Protected Sub ImageButton5_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton5.Click
        Dim OBJ As New Empresas
        Dim dt As DataTable = OBJ.GetRecords
        dt.TableName = "Empresas"
        Dim reporte As New ReportDocument
        reporte.Load(Publico.RutaRpt & "RptEmpresas.rpt")
        reporte.SetDataSource(dt)
        reporte.PrintToPrinter(1, True, 1, 0)
    End Sub

    Protected Sub ImageButton6_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton6.Click
        Dim OBJ As New Empresas
        Dim dt As DataTable = OBJ.GetRecords
        dt.TableName = "Empresas"
        Dim reporte As New ReportDocument
        reporte.Load(Publico.RutaRpt & "RptEmpresas.rpt")
        reporte.SetDataSource(dt)
        reporte.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, True, "Listado de Empresas")
    End Sub
End Class
